// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.security;

import lichen.LichenConstants;
import lichen.internal.security.SecurityCheckerImpl;
import lichen.internal.security.SecurityContextFilter;
import lichen.internal.security.SecurityWorker;
import lichen.services.UserService;

import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.services.ComponentClassTransformWorker;
import org.apache.tapestry5.services.RequestFilter;
import org.apache.tapestry5.services.RequestGlobals;

/**
 * 对安全进行检测的module
 * @author Jun Tsai
 * @version $Revision: 225 $
 * @since 0.0.3
 */
public class SecurityModule {
	/**
	 * 绑定使用的service.
	 * @param binder Service Binder
	 * @see ServiceBinder
	 */
	public static void bind(ServiceBinder binder) {
		binder.bind(SecurityChecker.class,SecurityCheckerImpl.class);
	}
	/**
	 * 对安全控制的过滤.
	 * 
	 * @param configuration  配置.
	 * @param requestGlobals request globals object. 
	 * @param userService  user service.
	 * @param encodingFilter 编码的过滤器.
	 */
	public void contributeRequestHandler(
			OrderedConfiguration<RequestFilter> configuration, RequestGlobals requestGlobals, UserService userService) {
		configuration.add("securityFilter", new SecurityContextFilter(requestGlobals,userService),"after:OpenSessionInThreadFilter");
	}
	/**
	 * 默认的encoding设置为UTF-8
	 * 
	 * @param configuration
	 *            配置.
	 */
	public void contributeApplicationDefaults(
			MappedConfiguration<String, String> configuration) {
		// 默认的配置页面
		configuration.add(LichenConstants.USER_LOGIN_PAGE, "user/login");
	}
	/**
	 * 对安全的控制的ClassTransform worker 
	 * @param configuration
	 * @param securityChecker
	 * @since 0.0.3
	 */
	public static void contributeComponentClassTransformWorker(
            OrderedConfiguration<ComponentClassTransformWorker> configuration,SecurityChecker checker) {
        configuration.add("security", new SecurityWorker(checker));
    }
}
